#include "menu.h"


int menu()
{
int i;

int choice=0;

BITMAP* buffer = create_bitmap(800,600);

	// Les images du menu

BITMAP* mainFrame[4];
mainFrame[0] = load_bitmap("./images/menu/menu0.bmp", NULL);
mainFrame[1] = load_bitmap("./images/menu/menu1.bmp", NULL);
mainFrame[2] = load_bitmap("./images/menu/menu2.bmp", NULL);
mainFrame[3] = load_bitmap("./images/menu/menu3.bmp", NULL);

	//Loading

if(started==0)
{
	BITMAP* load[6];
	load[0] = load_bitmap("./images/loading/load0.bmp", NULL);
	load[1] = load_bitmap("./images/loading/load1.bmp", NULL);
	load[2] = load_bitmap("./images/loading/load2.bmp", NULL);
	load[3] = load_bitmap("./images/loading/load3.bmp", NULL);
	load[4] = load_bitmap("./images/loading/load4.bmp", NULL);
	load[5] = load_bitmap("./images/loading/load5.bmp", NULL);


	for(i=0;i<6;i++)
	{
	blit(load[i],buffer,0,0,0,0,800,600);
	blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
	Sleep(500000);
	}

	for(i=0;i<6;i++)
	destroy_bitmap(load[i]);

	started = 1;
}


// initialisation de l'affichage avec le choix par défaut "play"

blit(mainFrame[choice],buffer,0,0,0,0,800,600);
blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);

    int panning = 128; // Balance du son "0 all left" "255 all right"
    int pitch = 1000;  // Vitesse de la lecture
    int volume = 255;  // entre 0 et 255

    SAMPLE *sample;

    sample = load_sample("sounds/menu.wav");

    if (!sample) 
    {
        allegro_message("Error reading wave file");
        return;
    }

    //play the sample with looping

    play_sample(sample, volume, panning, pitch, TRUE); // TRUE pour boucler la lecture
    


do
{
	readkey();
	if(key[KEY_UP])
	{
		if (choice>0)
		{
			choice--;
			blit(mainFrame[choice],buffer,0,0,0,0,800,600);
			blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
		}
		else
		if (choice==0)
		{
			choice = 3;
			blit(mainFrame[choice],buffer,0,0,0,0,800,600);
			blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
		}
	}
	if(key[KEY_DOWN])
	{
		if (choice<3)
		{
			choice++;
			blit(mainFrame[choice],buffer,0,0,0,0,800,600);
			blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
		}
		else
		if (choice==3)
		{
			choice = 0;
			blit(mainFrame[choice],buffer,0,0,0,0,800,600);
			blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
		}
		
	}
}while(!key[KEY_ENTER]);

for(i=0;i<3;i++)
destroy_bitmap(mainFrame[i]);

//destroy the sample
destroy_sample(sample);

return choice;

}
